//
//  YTOrderFilterView.m
//  YiTuDriverClient
//
//  Created by 石少庸 on 2017/6/1.
//  Copyright © 2017年 Yitu. All rights reserved.
//

#import "YTOrderFilterView.h"
#import "YTPickerView.h"
#import "YTOrderServiceCityModel.h"

@interface YTOrderFilterView ()
@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) IBOutlet UIView *cityView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bgViewHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *cityViewHeight;
@property (weak, nonatomic) IBOutlet UIButton *startBtn;
@property (weak, nonatomic) IBOutlet UIButton *endBtn;
@property (strong, nonatomic) NSMutableSet *productionSet;
@property (strong, nonatomic) NSMutableSet *carTypeSet;
@property (strong, nonatomic) NSMutableSet *citySet;
@property (strong, nonatomic) NSArray<YTOrderServiceCityModel *> *dataArray;
@end

@implementation YTOrderFilterView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self loadSubviews];
    }
    return self;
}

- (void)loadSubviews {
    UIView *view = [[NSBundle mainBundle] loadNibNamed:@"YTOrderFilterView" owner:self options:nil].firstObject;
    view.frame = self.bounds;
    [self addSubview:view];
    
    _dataArray = [NSMutableArray array];
    _productionSet = [NSMutableSet set];
    _carTypeSet = [NSMutableSet set];
    _citySet = [NSMutableSet set];
}

#pragma mark - 加载服务城市数据

- (void)loadCityData:(NSArray<YTOrderServiceCityModel *> *)dataSource {
    _dataArray = dataSource;
    _bgViewHeight.constant += (dataSource.count/3 * 45) + 10;
    _cityViewHeight.constant = (dataSource.count/3 + 1) * 45 + 15;
    
    // 创建城市按钮
    for (NSInteger i = 0; i < dataSource.count; i++) {
        UIButton *cityBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [cityBtn setTitle:dataSource[i].name forState:UIControlStateNormal];
        [cityBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        cityBtn.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [cityBtn setTitleColor:kMainBlueColor forState:UIControlStateSelected];
        cityBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        cityBtn.layer.cornerRadius = 3.0;
        cityBtn.tag = 1120 + i;
        [cityBtn setBackgroundImage:[UIImage imageNamed:@"public_button_check"] forState:UIControlStateSelected];
        [cityBtn addTarget:self action:@selector(selecteParameterAction:) forControlEvents:UIControlEventTouchUpInside];
        CGFloat height = 30;
        CGFloat width = 70;
        CGFloat spw = 15 + i%3 * (width + 15);
        CGFloat sph = 15 + i/3 * (height + 15);
        cityBtn.frame = CGRectMake(spw, sph, width, height);
        [_cityView addSubview:cityBtn];
    }
}

#pragma mark - Action

// 所有响应这个方法的button都有tag
// 产品类型 -> 1100...
// 接单车型 -> 1130...
// 所在城市 -> 1140...
// 开始时间 -> 1150...
- (IBAction)selecteParameterAction:(UIButton *)sender {
    sender.selected = !sender.selected;
    sender.backgroundColor = sender.selected ? [UIColor whiteColor] : [UIColor groupTableViewBackgroundColor];
    if (sender.tag > 1100 && sender.tag < 1110) {
        NSString *product = [NSString stringWithFormat:@"%zd", sender.tag - 1000];
        sender.selected ? [_productionSet addObject:product] : [_productionSet removeObject:product];
    } else if (sender.tag >= 1120 && sender.tag < 1300) {
        NSString *city_id = _dataArray[sender.tag - 1120].city_id;
        sender.selected ? [_citySet addObject:city_id] : [_citySet removeObject:city_id];
    } else {
        NSString *carType = [NSString stringWithFormat:@"%zd", sender.tag - 1110];
        sender.selected ? [_carTypeSet addObject:carType] : [_carTypeSet removeObject:carType];
    }
}

- (IBAction)selecteTimeAction:(UIButton *)sender {
    YTPickerView *picker = [YTPickerView pickerWithType:YTPickerTypeMonthDay];
    picker.dimmedMask = YES;
    
    if (_isShowPastOrder) {
        picker.birthday = YES;
    }
    
    picker.completion = ^(NSString *selectString) {
        [sender setTitle:selectString forState:UIControlStateNormal];
    };
    [picker show];
}

- (IBAction)resetAction:(UIButton *)sender {
    for (UIView *view in _bgView.subviews) {
        [self changeButtonState:view];
    }
    
    for (UIView *view in _cityView.subviews) {
        [self changeButtonState:view];
    }
    
    [_startBtn setTitle:@"开始时间" forState:UIControlStateNormal];
    _startBtn.backgroundColor = [UIColor whiteColor];
    [_endBtn setTitle:@"结束时间" forState:UIControlStateNormal];
    _endBtn.backgroundColor = [UIColor whiteColor];
    
    if (_resetAction) {
        _resetAction();
    }
}

- (void)changeButtonState:(UIView *)view {
    if ([view isKindOfClass:[UIButton class]]) {
        UIButton *button = (UIButton *)view;
        button.selected = NO;
        button.backgroundColor = [UIColor groupTableViewBackgroundColor];
    }
}

- (IBAction)completeAction:(UIButton *)sender {
    if (_completeAction) {
//        NSLog(@"%@\n%@\n%@", _productionSet, _carTypeSet, _citySet);
        NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
        dict[@"seatType"] = [self enumeratorDataSet:_carTypeSet];
        dict[@"subType"] = [self enumeratorDataSet:_productionSet];
        dict[@"serviceCity"] = [self enumeratorDataSet:_citySet];
        if (_startBtn.currentTitle.length > 4) {
            dict[@"useTimeBegin"] = _startBtn.currentTitle;
        } else {
            dict[@"useTimeBegin"] = @"";
        }
        
        if (_endBtn.currentTitle.length > 4) {
            dict[@"useTimeEnd"] = _endBtn.currentTitle;
        } else {
            dict[@"useTimeEnd"] = @"";
        }
        _completeAction(dict);
    }
}

// 拼接筛选参数
- (NSString *)enumeratorDataSet:(NSMutableSet<NSString *> *)set {
    if (!set.count) return @"";
    
    __block NSString *temp = @"";
    [set enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, BOOL * _Nonnull stop) {
        temp = [temp stringByAppendingString:[NSString stringWithFormat:@", %@", obj]];
    }];
    
    return [temp substringFromIndex:2];
}

@end
